package com.eastchina.fids2focs.common;

import org.apache.commons.lang.StringUtils;
import org.codehaus.xfire.transport.http.XFireServletController;
import org.springframework.beans.factory.annotation.Autowired;

import com.eastchina.fids2focs.common.exception.FIDSIIValidateException;
import com.eastchina.fids2focs.domain.SystemUser;
import com.eastchina.fw.database.dao.QueryDAO;

/**
 * 权限管理类
 * @author xiayg
 *
 */
public class AuthenticationManager {
	
	/**
	 * 查询DAO
	 */
	@Autowired
	QueryDAO queryDAO;

	/**
	 * 权限管理
	 * @param username 用户名
	 * @param password 密码
	 */
	public SystemUser checkPermission(String username, String password, String macAddr) throws FIDSIIValidateException {
		
		/*
		 * 身份验证
		 */
		// 获取指定用户的用户情报
		SystemUser user = queryDAO.executeForObject("SYSTEM_USER.select", username, SystemUser.class);
		if (user == null) {
			throw new FIDSIIValidateException(new FIDSIIError("sysmgr.AE1003"));
		}
		// 密码一致判断֤
		if (!StringUtils.equals(password, user.getPasswd())) {
			throw new FIDSIIValidateException(new FIDSIIError("sysmgr.AE1004"));
		}
		// MAC Address
		if (!StringUtils.isEmpty(user.getMacAddr()) && !StringUtils.equals(macAddr, user.getMacAddr())) {
			throw new FIDSIIValidateException(new FIDSIIError("sysmgr.AE1006"));
		}
		
		// save UserObject
		XFireServletController.getRequest().getSession().setAttribute(GlobalConstants.USER_KEY, user);
		
		return user;
	}	
}
